package com.atguigu.gmall.web.controller;/*
 * @author: XueYouPeng
 * @time: 23.8.2 下午 9:09
 */

import com.atguigu.gmall.common.feign.seckill.SeckillFeignClient;
import com.atguigu.gmall.common.feign.user.UserFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.atguigu.gmall.user.entity.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    private SeckillFeignClient seckillFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping(value = "/seckill.html")
    public String seckill(Model model){
        Result<List<SeckillGoods>> result = seckillFeignClient.findAll();
        List<SeckillGoods> goodsList = result.getData();
        model.addAttribute("list" , goodsList);
        return "seckill/index";
    }

    @GetMapping(value = "/seckill/{skuId}.html")
    public String seckillItem(@PathVariable(value = "skuId") Long skuId , Model model){
        Result<SeckillGoods> goodsResult = seckillFeignClient.findBySkuId(skuId);
        SeckillGoods seckillGoods = goodsResult.getData();
        model.addAttribute("item" , seckillGoods);
        return "seckill/item";
    }

    @GetMapping(value = "/seckill/queue.html")
    public String queue(@RequestParam(value = "skuId") Long skuId , @RequestParam(value = "skuIdStr") String skuIdStr, Model model) {
        model.addAttribute("skuId" , skuId) ;
        model.addAttribute("skuIdStr" , skuIdStr) ;
        return "seckill/queue" ;
    }

    @GetMapping(value = "/seckill/trade.html")
    public String trade(@RequestParam(value = "skuId") Long skuId , Model model){

        //查询用户收件地址
        Result<List<UserAddress>> userAddressListResult = userFeignClient.findUserAddressByUserId();
        List<UserAddress> userAddressList = userAddressListResult.getData();

        //查询订单的数据
        Result<OrderInfo> orderInfoResult = seckillFeignClient.findOrderInfoBySkuId(skuId);
        OrderInfo orderInfo = orderInfoResult.getData();


        model.addAttribute("userAddressList" , userAddressList);
        model.addAttribute("detailArrayList" , orderInfo.getOrderDetailList());
        model.addAttribute("totalNum" , 1);
        model.addAttribute("totalAmount" , orderInfo.getTotalAmount());

        return "seckill/trade";
    }
}
